﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CH.Easy.DbProvider.Core;
using CH.Easy.UI.Common;
using CH.Easy.DbProvider.Interface;
using System.Linq.Expressions;
using CH.Easy.DbProvider.Entity;

namespace CH.Easy.UI.Repository
{
    public class AbstractRepository<TEntity> : IRepository<TEntity> where TEntity : Entity
    {
        private DbContext Context = new DbContext();
        protected IDbTable<TEntity> Table { get; set; }
        protected TSQLEngine TSQLEngine { get; set; }
        public AbstractRepository()
        {
            this.Table = Context.GetTable<TEntity>();
            this.TSQLEngine = Context.TSQLEngine;
        }
        protected IDbTable<T> GetTable<T>()
        {
            return Context.GetTable<T>();
        }
        public object Add(TEntity item)
        {
            return this.Table.Add(item);
        }
        public int Remove(Expression<Func<TEntity, bool>> s)
        {
            return this.Table.Remove(s);
        }

        public int Modify(TEntity item, Expression<Func<TEntity, bool>> s, params string[] modifyColumns)
        {
            return this.Table.Modify(item, s, modifyColumns);
        }

        public TEntity GetByFilter(Expression<Func<TEntity, bool>> filter)
        {
            return this.Table.GetByFilter(filter);
        }

        public TResult GetByFilter<TResult>(Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TResult>> selector)
        {
            return this.Table.GetByFilter(filter, selector);
        }

        public List<TEntity> GetAll()
        {
            return this.Table.GetAll();
        }

        public List<TEntity> GetListByPage(int pageIndex, int pageSize, out int totalRecord)
        {
            QueryResult<TEntity> result = this.Table.GetListByPage(pageIndex, pageSize);
            totalRecord = result.TotalRecord;
            return result.List;
        }

        public List<TEntity> GetListByPage(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> filter, out int totalRecord)
        {
            QueryResult<TEntity> result = this.Table.GetListByPage(pageIndex, pageSize, filter);
            totalRecord = result.TotalRecord;
            return result.List;
        }

        public List<TResult> GetListByPage<TResult>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TResult>> selector, out int totalRecord)
        {
            QueryResult<TResult> result = this.Table.GetListByPage(pageIndex, pageSize, filter, selector);
            totalRecord = result.TotalRecord;
            return result.List;
        }

        public List<TResult> GetListByPage<TResult, TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, TKey>> orderByExpression, bool ascending, out int totalRecord)
        {
            QueryResult<TResult> result = this.Table.GetListByPage(pageIndex, pageSize, filter, selector, orderByExpression, ascending);
            totalRecord = result.TotalRecord;
            return result.List;
        }

        public List<TEntity> GetListByFilter(Expression<Func<TEntity, bool>> filter)
        {
            return this.Table.GetListByFilter(filter);
        }

        public List<TResult> GetListByFilter<TResult>(Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TResult>> selector)
        {
            return this.Table.GetListByFilter(filter, selector);

        }

        public List<TResult> GetListByFilter<TResult, TKey>(Expression<Func<TEntity, bool>> filter, Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, TKey>> orderByExpression, bool ascending)
        {
            return this.Table.GetListByFilter(filter, selector, orderByExpression, ascending);
        }




        public void Dispose()
        {
            this.Context = null;
            this.TSQLEngine = null;
            this.Table = null;
        }
    }
}
